const utils = require('./util.js')
const upload = (tempFilePath, album_code)=>{
  const TOKEN = wx.getStorageSync('usertoken');
  //上传图片先向服务器请求签名
  return new Promise((resolve,reject)=>{
    wx.request({
      url:'https://yimiao-prd.e-hqins.com/res/album/oss_getsignature',
      method: 'post',
      header: {
        'Content-Type': 'application/x-www-form-urlencoded;charset=UTF-8',
        'Authorization': TOKEN
      },
      data: {
        album_code
      },
      success: function(res){
        if(res.data.code == 200){
          let msg = res.data.data;
          let aliyunFileKey = msg.dir + '/' + utils.wxuuid() + tempFilePath.substring(tempFilePath.lastIndexOf("."));
          console.log(aliyunFileKey)
          wx.uploadFile({
            url: msg.geturl,//上传的路径
            filePath: tempFilePath,
            name: 'file',
            formData: {
              key: aliyunFileKey,//上传图片的名字和路径（默认路径根目录，自定义目录：xxx/xxx.png）
              policy: msg.policy,
              OSSAccessKeyId: msg.oss_access_key_id,
              success_action_status: "200",
              signature: msg.signature,
              callback: msg.callback,
            },
            success: function (res) {
              console.log(`上传图片success回调`)
              console.log(res)
              if (res.statusCode == 200){
                let data = JSON.parse(res.data);
                console.log(data)
                resolve(data)
              }else{
              wx.showToast({
                title: "上传失败",
                icon: 'none',
                duration: 1000
              })
              }
            },
            fail:reject,
          })
        }
      },
      fail: reject
    })
  })
 
}



export default upload